# -*- coding:utf-8 -*-
# @FileName :response.py
# @Date: 2024/7/31
# @Author:天空之城
from typing import List


class CodeStatus:
    SUC = 2_00_00_00  # success
    ERR = 4_00_00_00  # client error
    EXC = 5_00_00_00  # internal exception
    MIS_CONTACT = 4_00_03_02  # missing contact info


class CodeBusiness:
    DEFAULT = 0


class CodeOperation:
    DEFAULT = 0
    CREATE = 1_00
    READ = 2_00
    UPDATE = 3_00
    DELETE = 4_00
    RECREATE = 5_00
    SYNC = 6_00  # sync create task
    MISS = 7_00
    INVALID = 8_00
    REVOKE = 9_00
    EXPIRE = 10_00
    UPLOAD = 11_00


class CodeAny:
    DEFAULT = 0


class Base:
    Code = CodeStatus.EXC + CodeBusiness.DEFAULT + CodeOperation.DEFAULT + CodeAny.DEFAULT
    Status = 500
    Message = "服务器内部错误或网络错误"

    def __init__(self, message=None):
        self.code = self.Code
        self.message = message if message else self.Message
        self.status = self.Status

    def asdict(self):
        return dict(code=self.Code, status=self.Status, message=getattr(self, "message", self.Message))


class Response(Base):
    Code = CodeStatus.SUC + CodeBusiness.DEFAULT + CodeOperation.DEFAULT + CodeAny.DEFAULT
    Status = 200
    Message = "请求成功"

    def __init__(self, message=None):
        self.items = None
        self.total = None
        self.item = None
        self.data = None
        super().__init__(message)

    def set_item(self, item):
        self.item = item
        return self

    def set_data(self, data):
        self.data = data
        return self

    def set_items(self, items: List, total: int = None):
        self.items = items
        self.total = total if total is not None else len(items)
        return self

    def asdict(self):
        ret = super().asdict()
        if self.item is not None:
            ret["item"] = self.item
        elif self.items is not None:
            ret["items"] = self.items
            ret["total"] = self.total
        return ret


class AddressParameterError(Response):
    Message = "地址参数传递错误，请检查参数传递！"


class InvalidAddressError(Response):
    Message = "无效地址，请检查地址的有效性！"


class RF:
    """
    response factory 工厂类 方便构造response
    """

    @staticmethod
    def success(data: any = None, message="请求成功", code: int = CodeStatus.SUC, status: int = 200):
        return dict(message=message, code=code, data=data, status=status)

    @staticmethod
    def success_address(StandardAddress: any = None, Msg="请求成功", StatusCode: int = 200):
        return dict(StandardAddress=StandardAddress, Msg=Msg, StatusCode=StatusCode)

    @staticmethod
    def success_item(item: any, message="请求成功", code: int = CodeStatus.SUC, status: int = 200):
        """
        构造item
        """
        return RF.success(dict(item=item), message, code, status)


class EF:
    """
    error factory 工厂类 方便构造error
    """

    @staticmethod
    def error(data: any = None, message="请求失败", code: int = CodeStatus.ERR, status: int = 500):
        return dict(message=message, code=code, data=data, status=status)

    @staticmethod
    def error_address(StandardAddress: any = None, Msg="无效地址！", StatusCode: int = 500):
        return dict(Msg=Msg, StandardAddress=StandardAddress, StatusCode=StatusCode)

    @staticmethod
    def error_item(item: any, message="请求失败", code: int = CodeStatus.ERR, status: int = 500):
        """
        构造item
        """
        return EF.error(dict(item=item), message, code, status)
